package tester;

import java.util.ArrayList;

import calculateDistance.LineEquation;
import pointAndLine.Point;
import returnTypes.FinalValues;
import returnTypes.Location;
import doorAndWindow.Door;
import doorAndWindow.OnWallObject;
import doorAndWindow.Window;
import wall.Wall;


public class DoorWindowTester {
	private Wall testWall =null;
	private final int maxY=FinalValues.getMaxY();
	private final int maxX=FinalValues.getMaxX();
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private int capitation;
	private boolean succes;
	private int[][] testDoorWindowMatrix = new int[maxX][maxY];

	public void insertOnWallObject(OnWallObject object,Wall drawWall,Location loc)
	{
		succes=false;
		if (object instanceof Door) capitation='D';
		else if (object instanceof Window) capitation='W';
		else return;
		setXY(drawWall);
		if(loc==Location.LEFT) drawLeft(object,drawWall);
		if(loc==Location.CENTER) drawCenter(object,drawWall);
		if(loc==Location.RIGHT) drawRight(object,drawWall);

	}
	
	private void setXY(Wall drawWall)
	{
		x1=drawWall.getBaseLine().getPoint1().getCmX();
		y1=drawWall.getBaseLine().getPoint1().getCmY();
		x2=drawWall.getBaseLine().getPoint2().getCmX();
		y2=drawWall.getBaseLine().getPoint2().getCmY();
		line(x1,y1,x2,y2,1);
	}
	
	private void setXY(Point p1,Point p2)
	{
		x1=p1.getCmX();
		y1=p1.getCmY();
		x2=p2.getCmX();
		y2=p2.getCmY();
	}
	
	private void drawCenter(OnWallObject object,Wall drawWall)
	{
		
		ArrayList<Point> points=new ArrayList<Point>();
		Point centerPoint=new Point((x1+x2)/20.0,(y1+y2)/20.0);
		System.out.println(centerPoint.toString());
		LineEquation lE=new LineEquation(object.getWidth()/2-0.05);
		System.out.println("DISTANCE  "+lE.getDistance());
		lE.calculateValues(drawWall.getBaseLine().getPoint1(),drawWall.getBaseLine().getPoint2());
		if (lE.verifyEquation(centerPoint))
			{ 
				succes=true;
				lE.setValues(centerPoint);
				points=lE.solveGrade2Equation(); 
				setXY(points.get(0),points.get(1)); 
				System.out.println(points.get(0).toString());
				System.out.println(points.get(1).toString());
				doorLine(x1,y1,x2,y2,capitation,1);
				System.out.println();
				printMatrix();

			} 
		else System.out.print("Fail");
	}
	
	public int[][] getTestDoorWindowMatrix() {
		return testDoorWindowMatrix; 
	}

	public boolean isSucces() {
		return succes;
	}

	private void drawRight(OnWallObject object,Wall drawWall)
	{
		
	}
	
	private void drawLeft(OnWallObject object,Wall drawWall)
	{
		
	}
	
	public void setTestWall(Wall testWall) {
		this.testWall = testWall;
	}

	public Wall getTestWall() {
		return testWall;
	}
	
	public int getCapitation() {
		return capitation;
	}

	public void setCapitation(int capitation) {
		this.capitation = capitation;
	}
	
	public void printMatrix()
	{
		for(int i=0;i<maxX;i++){
			for(int j=0;j<maxY;j++)
				if (testDoorWindowMatrix[i][j]<10)System.out.print(testDoorWindowMatrix[i][j]);
				else System.out.print((char)testDoorWindowMatrix[i][j]);
		System.out.println();
		}
	}
	
	private void doorLine(int x,int y,int x2, int y2, int value,int oldValue) {
	    int w = x2 - x ;
	    int h = y2 - y ;
	    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
	    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
	    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
	    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
	    int longest = Math.abs(w) ;
	    int shortest = Math.abs(h) ;
	    if (!(longest>shortest)) {
	        longest = Math.abs(h) ;
	        shortest = Math.abs(w) ;
	        if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
	        dx2 = 0 ;            
	    }
	    int numerator = longest >> 1 ;
	    for (int i=0;i<=longest;i++) {
	    	for( int thisX=x-1;thisX<=x+1;thisX++)
	    		for( int thisY=y-1;thisY<=y+1;thisY++)
	    			if (testDoorWindowMatrix[thisX][thisY]==oldValue) testDoorWindowMatrix[thisX][thisY]=value;
	        numerator += shortest ;
	        if (!(numerator<longest)) {
	            numerator -= longest ;
	            x += dx1 ;
	            y += dy1 ;
	        } else {
	            x += dx2 ;
	            y += dy2 ;
	        }
	    }
    } 
	private void line(int x,int y,int x2, int y2, int value) {
	    int w = x2 - x ;
	    int h = y2 - y ;
	    int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0 ;
	    if (w<0) dx1 = -1 ; else if (w>0) dx1 = 1 ;
	    if (h<0) dy1 = -1 ; else if (h>0) dy1 = 1 ;
	    if (w<0) dx2 = -1 ; else if (w>0) dx2 = 1 ;
	    int longest = Math.abs(w) ;
	    int shortest = Math.abs(h) ;
	    if (!(longest>shortest)) {
	        longest = Math.abs(h) ;
	        shortest = Math.abs(w) ;
	        if (h<0) dy2 = -1 ; else if (h>0) dy2 = 1 ;
	        dx2 = 0 ;            
	    }
	    int numerator = longest >> 1 ;
	    for (int i=0;i<=longest;i++) {
	    	testDoorWindowMatrix[x][y]=value;
	        numerator += shortest ;
	        if (!(numerator<longest)) {
	            numerator -= longest ;
	            x += dx1 ;
	            y += dy1 ;
	        } else {
	            x += dx2 ;
	            y += dy2 ;
	        }
	    }
    } 

}
